package com.duckygo.towerdefense.game.sprites;

import com.duckygo.towerdefense.game.scenes.TowerDefenseScene;

public class Projectile extends Sprite2D {

	
	private Unit target;
	
	private int damage;
	
	private float speed = 6;

	
	
	public Projectile(Tower tower, Unit target, int damage) {
		x = tower.x;
		y = tower.y;
		
		anchor = CENTER;
		
		this.damage = damage;
		this.target = target;
		
		width = 16;
		height = 16;
		
		texture = TowerDefenseScene.FIREBALL_TEX;
	}
	
	

	@Override
	public void update(long time) {
		
		int distX = (int) (x - target.x);
		int distY = (int) (y - target.y);
		
		if (Math.abs(distX) < 10 && Math.abs(distY) < 10 && parent != null) {
			parent.addChild(new Explosion(this));
			
			target.getHealth().hurt(damage);
		}
		
		x -= Math.signum(distX) * speed;
		y -= Math.signum(distY) * speed;
	}
}
